<?php
declare(strict_types=1);

namespace Modules\Promanagement\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Promanagement\Models\Processflow;
use Modules\Promanagement\Models\Processdata;
use Modules\Promanagement\Models\Provflowsional;
use Illuminate\Http\Request;
use Catch\Exceptions\FailedException;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Illuminate\Support\Facades\DB;
use Modules\Promanagement\Http\Repositories\ProcessflowRequest;

class ProcessflowController extends Controller
{
    public function __construct(
        protected readonly Processflow $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
    {
        
        $data = $this->model->getList();
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        foreach($data as $k=>$v){
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
            if(!empty($v['purchaseorder_id'])){
               $Processdata =  Processdata::whereIn('id',$v['purchaseorder_id'])->get();
               $Processlist = '';
               $data[$k]['detailscheckAll'] = $Processdata;
               foreach($Processdata as $ks=>$vs){
                    $Processlist .= $vs['processname'].'-';  
               }
               $data[$k]['processdata'] = substr($Processlist,0,strlen($Processlist)-1);;
            }
       }
        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        
        $data = $request->all();
        $res = $this->model->where('pflcode',$data['pflcode'])->first();
        if(!empty($res)){
            throw new FailedException('工艺流程代号已存在请重新录入');
         }else{
            $data = ProcessflowRequest::create($data);
            return $data;
         }
        
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
    
        $data = $this->model->firstBy($id);
    
        $dataProvflowsional = Provflowsional::where('processflow_id',$data['id'])->get()->toArray();
       
        foreach($dataProvflowsional as $k=>$v){
            $processname =  Processdata::where('id',$v['processdata_id'])->first();
            if(!empty($processname)){
                 $dataProvflowsional[$k]['processname'] = $processname->processname;
            }else{
                $dataProvflowsional[$k]['processname'] = '';
            } 
        }
        $data['detailscheck_all'] = $dataProvflowsional;
      
      
        return $data;
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    { 
        $data = $request->all();
        
        $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
        return ProcessflowRequest::createUpdate($id,$data);
        // return $this->model->updateBy($id, $data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }
      /**
     * @param $id
     * @return mixed
     */
    public function Detailinterface($id)
    {
        $data = $this->model->firstBy($id);
        $data['detailscheckAll'] = Processdata::whereIn('id',$v['purchaseorder_id'])->get();
        return $data;
    }



}
